/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: data_manager_proxy.cpp
 */

#include <boost/asio.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>

#include "data_manager_proxy.hpp"
#include "data.hpp"

using namespace middleware;

//-----------------------------------------------------------------------------
// Constructors and destructor
/* Private constructor */
DataManagerProxy::DataManagerProxy() {
	this->_config.ip_bind_w = "127.0.0.1";
	this->_config.port_bind_w = "0";
	this->_config.ip_to_w = "127.0.0.1";
	this->_config.port_to_w = "0";
}
/* Private constructor */
DataManagerProxy::DataManagerProxy(DataProxyConfig conf) {
	this->_config.ip_bind_w = conf.ip_bind_w;
	this->_config.port_bind_w = conf.port_bind_w;
	this->_config.ip_to_w = conf.ip_to_w;
	this->_config.port_to_w = conf.port_to_w;
}
/* Destructor */
DataManagerProxy::~DataManagerProxy() {

}
//-----------------------------------------------------------------------------
// Public members
/* Accept and send data */
void DataManagerProxy::send_data(Data data) throw(ProxyException) {
	using boost::asio::ip::tcp;
	// Managing destination unavailable
	// In this place we consider delivery errors.
	for (int i = 10; true; i--) {
		try {
			// Sending data
			tcp::iostream data_stream(this->_config.ip_to_w,
					this->_config.port_to_w); /* Creating TCP stream */
			if (!data_stream) {
				// Problem in sending...
				throw ProxyException("Unable to open TCP stream!");
			} else {
				// OK we can send it
				boost::archive::binary_oarchive oa(data_stream);
				oa << data; /* Sending --> SENT */
				data_stream.close();
				return; /* Get away */
			}
		} catch(ProxyException& e) {
			// Could not reach destination: try again
			if (i <= 0) throw ProxyException(std::string(
					"Error in proxy: data communication to Worker failed: ") +
					e.what());
			boost::this_thread::sleep(boost::posix_time::milliseconds(100));
		} catch(std::exception& e) {
			// Unknown exception
			throw ProxyException(
					std::string("Not reversible error in proxy for UI/Worker communication: ") +
					e.what());
		}
	}
}
/* Request data */
Data DataManagerProxy::recv_data() throw(ProxyException) {
	using boost::asio::ip::tcp;
	int port = boost::lexical_cast<int>(this->_config.port_bind_w);
	boost::asio::io_service io_s;
	try {
		tcp::acceptor data_acceptor(io_s, tcp::endpoint(tcp::v4(), port));
		tcp::iostream data_stream;
		Data data_recv;
		data_acceptor.accept(*(data_stream.rdbuf())); /* Accepting */
		boost::archive::binary_iarchive ia(data_stream);
		ia >> data_recv; /* Here I have the request (in the form of a dummy data) */
		data_stream.close();
		return data_recv;
	} catch(std::exception& e) {
		throw ProxyException(std::string("Error in proxy: data communication to Worker failed: ") +
				e.what());
	}
}
